package com.united.ucts.modules.juc.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author hudan
 * @version 1.0
 * @filename LTick.java
 * @copyright <a href="www.chancein.com">www.chancein.com</a>
 * @date 2022-09-19
 */
public class LTick {

    private int tickNum = 30;
    private Lock lock = new ReentrantLock();

    public void sale() {
        lock.lock();
        try {
            while (tickNum > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出了第:" + (tickNum--) + "张票");
            }
        } finally {
            lock.unlock();
        }
    }


    public static void main(String[] args) {
        // 创建资源类
        LTick tick = new LTick();

        // 创建线程
        new Thread(() -> {
            // 当前线程需要处理的业务逻辑
            for (int i = 0; i < 30; i++) {
                tick.sale();
            }
        }, "AA").start();
        new Thread(() -> {
            // 当前线程需要处理的业务逻辑
            for (int i = 0; i < 30; i++) {
                tick.sale();
            }
        }, "BB").start();
        new Thread(() -> {
            // 当前线程需要处理的业务逻辑
            for (int i = 0; i < 30; i++) {
                tick.sale();
            }
        }, "CC").start();
    }
}
